package com.tierstall.client.view.content;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import com.tierstall.client.presenter.DBFilterPresenter;
import com.tierstall.client.presenter.LoginPresenter;
import com.tierstall.client.view.main.ContentWidget;
import com.tierstall.shared.GruppeDTO;
import com.tierstall.shared.Logger;
import com.tierstall.shared.Page;
import com.tierstall.shared.SearchQuery;
import com.tierstall.shared.UserType;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.event.dom.client.ClickEvent;

public class CV_dbFilter extends ContentWidget {
	public interface AbstractDBFilterPresenter {
		// Presenter Methods here
		void onSearchClicked(SearchQuery query);
	}

	private AbstractDBFilterPresenter presenter;
	private ArrayList<GruppeDTO> result;

	public void setPresenter(DBFilterPresenter presenter) {
		this.presenter = presenter;
	}

	// standard stuff
	/**
	 * The UiBinder interface used by this example.
	 */
	interface Binder extends UiBinder<Widget, CV_dbFilter> {
	}

	@UiField
	ListBox combo_leiter;
	@UiField
	CheckBox chkBox_alteTiere;
	@UiField
	DateBox date_start;
	@UiField
	DateBox date_end;
	@UiField
	ListBox combo_gender;
	@UiField
	Label combo;
	@UiField
	ListBox combo_group;
	@UiField
	ListBox combo_type;

	public CV_dbFilter() {
		super("Datenbanksuche", "Suchergebnisse Filtern", false,
				UserType.VIEWER, Page.DB_FILTER);
	}

	public void fillComboAK(ArrayList<GruppeDTO> result) {
		this.result = result;
		combo_group.clear();
		combo_group.addItem("Alle");
		for (int i = 0; i < result.size(); ++i) {
			combo_group.addItem(result.get(i).getGenehmigung() + " / PL: "
					+ result.get(i).getLeiter(), result.get(i).getUuid());
		}
	}

	@Override
	public Widget onInitialize() {
		// Create the UiBinder.
		Binder uiBinder = GWT.create(Binder.class);
		Widget widget = uiBinder.createAndBindUi(this);
		return widget;
	}

	@Override
	protected void asyncOnInitialize(final AsyncCallback<Widget> callback) {
		GWT.runAsync(CV_dbFilter.class, new RunAsyncCallback() {

			public void onFailure(Throwable caught) {
				callback.onFailure(caught);
			}

			public void onSuccess() {
				callback.onSuccess(onInitialize());
			}
		});

	}

	@UiHandler("btn_show_db")
	void onBtn_show_dbClick(ClickEvent event) {
		GruppeDTO group = null;
		if (!combo_group.getItemText(combo_group.getSelectedIndex()).equals(
				"Alle")) {
			group = result.get(combo_group.getSelectedIndex() - 1);
			Logger.CLIENT_INFO("DBFilter: Selected index: "
					+ combo_group.getSelectedIndex()
					+ " Group: "
					+ result.get(combo_group.getSelectedIndex() - 1)
							.getGenehmigung() + " Start Date: "
					+ date_start.getValue() + " End Date: "
					+ date_end.getValue());
		}

		SearchQuery query = new SearchQuery(group, null, null,
				date_start.getValue(), date_end.getValue(), null, null, null,
				null, -1);
		if (presenter != null)
			presenter.onSearchClicked(query);
	}
}
